package com.mew.chess.core.game.lh;


import com.mew.chess.core.common.handler.ChessHandlerMapCmdType;
import com.mew.chess.core.common.handler.ChessPlayAnnotation;
import com.mew.chess.core.common.handler.ChessPlayHandler;
import com.mew.chess.core.common.message.request.BetRequestMessage;
import com.mew.chess.core.common.message.response.ChipMessage;
import com.mew.chess.core.game.lh.game.LhGameManager;
import com.mew.chess.core.game.lh.game.LhRoomManager;
import com.mew.chess.core.game.lh.game.common.BrnnConstantValue;
import com.mew.chess.core.game.lh.game.common.LhLinkType;
import com.mew.chess.core.game.lh.game.message.LhPushCMDMapType;
import com.mew.chess.core.game.lh.game.message.LhUserMessage;
import com.mew.common.dc.redis.AucRedisService;
import com.mew.common.exception.ResponseCode;
import com.mew.common.rest.bo.GameSession;
import com.zebra.remoting.protocol.ZebraCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@ChessPlayAnnotation(ChessHandlerMapCmdType.LhBet)
@Service
@Slf4j
public class LhBetHandler implements ChessPlayHandler<BetRequestMessage> {

    @Autowired
    private AucRedisService aucRedisService;


    public ResponseCode betMaxLimit(long bet, long betMax, LhUserMessage userVO) {

        if (userVO.getBets().stream().mapToLong(ChipMessage::getBetNumber).sum() + bet > betMax) {
            return ResponseCode.BetMax;
        }
        if (userVO.getBets().stream().mapToLong(ChipMessage::getBetNumber).sum() + bet > userVO.getGoldNumber()) {
            return ResponseCode.GoldNo;
        }
        return ResponseCode.Success;
    }

    @Override
    public void handle(ZebraCommand<BetRequestMessage> command, GameSession gameSession) {

        if (command.getRequest().getBetType() < 0 || command.getRequest().getBetNumber() < 1) {
            command.sendFailureCode(ResponseCode.ParamInvalid.code());
            return;
        }

        long userId = gameSession.getUserId();
        int roomId = gameSession.getRoomId();
        LhRoomManager roomManager = LhGameManager.getInstance().getController(roomId);
        LhUserMessage userMessage = roomManager.getPlayer(userId);
        if (null == roomManager || null == userMessage) {
            log.error("LHRoomManager is null userId:{} roomId: {}", userId, roomId);
            command.sendFailureCode(ResponseCode.LoginUn.code());
            return;
        }

        if (roomManager.getLinkType() != LhLinkType.Start.code()) {
            log.error("LHRoomManager stop bet userId:{} roomId: {}", userId, roomId);
            command.sendFailureCode(ResponseCode.StopBet.code());
            return;
        }


        ResponseCode responseCode = betMaxLimit(command.getRequest().getBetNumber(), roomManager.getBetMax(), userMessage);
        if (responseCode.code() != ResponseCode.Success.code()) {
            command.sendFailureCode(responseCode.code());
            return;
        }


        roomManager.addBetRecord(userId, command.getRequest().getBetType(), command.getRequest().getBetNumber());
        //参与
        aucRedisService.chessBetLock(userId, BrnnConstantValue.OVER_TIME); //参与
        command.sendMessage(command.getRequest());
        //真人押注推送
        roomManager.sendMsg(roomManager.getBetInfoMessage(), LhPushCMDMapType.BetInfoMessage.code(), 0);


    }
}
